#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "qprocess.h"
#include "QMessageBox"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //添加图片格式选项
    QStringList listPicFormatItems = {"webp","png","jpg","jpeg","tiff"};
    ui->comboBox_PicFormat->addItems(listPicFormatItems);

    ui->radioButton_PicToPDF->setChecked(true);
    ui->plainTextEdit->clear();
    ui->radioButton_No->setChecked(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

//单击“执行”按钮消息处理函数
void MainWindow::on_pushButton_clicked()
{
    ui->plainTextEdit->clear();

    /*---图片转PDF---*/
    if(ui->radioButton_PicToPDF->isChecked()){
        QString strPDF;
        if(ui->radioButton_No->isChecked()){
            strPDF = "1";
        }

        if(ui->radioButton_Yes->isChecked()){
            strPDF = "2";
        }

        QString strShellScriptPath = "./shell/pictures_to_pdf_v1.0.sh";
        QStringList arguments;
        arguments << strPDF; // 添加参数
        QProcess process;
        process.start(strShellScriptPath,arguments); // 调用Linux Shell脚本

        /*
        // 等待脚本启动
        if (!process.waitForStarted()){
            // 处理错误
            QString strError = process.errorString();
            //qDebug() << strError;
            ui->plainTextEdit->setPlainText("Error starting process:\r\n" + strError + "\r\nTimeout!");
        }
        */

        // 等待脚本结束
        if (!process.waitForFinished(-1))
        {
            // 处理错误
            QString strError = process.errorString();
            //qDebug() << strError;
            ui->plainTextEdit->setPlainText("Error waiting for process to finish:\r\n" + strError + "\r\nTimeout!");
            QMessageBox::critical(this, " 错误提示", " 超时，转换失败！");
        }else{
            QString strOutput = process.readAllStandardOutput();
            //qDebug() << strOutput;
            ui->plainTextEdit->setPlainText(strOutput);
            QMessageBox::information(this, "消息提示", "恭喜，转换成功！");
        }

        process.close();
    }

    /*---图片转图片---*/
    if(ui->radioButton_PicToPic->isChecked()){
        QString strPicFormat=ui->comboBox_PicFormat->currentText();
        //qDebug() << strPicFormat;

        QString strWatermarkText=ui->lineEdit_WatermarkText->text();
        //qDebug() << strWatermarkText;

        QString strWatermarkPosition="0";

        if(ui->radioButton_northwest->isChecked()){
            strWatermarkPosition = "1";
        }

        if(ui->radioButton_northeast->isChecked()){
            strWatermarkPosition = "2";
        }

        if(ui->radioButton_southwest->isChecked()){
            strWatermarkPosition = "3";
        }

        if(ui->radioButton_southeast->isChecked()){
            strWatermarkPosition = "4";
        }

        if(ui->radioButton_center->isChecked()){
            strWatermarkPosition = "5";
        }

        //qDebug() << strWatermarkPosition;

        //--------------------------------------------------------
        if(strWatermarkText.isEmpty()){
            QString strShellScriptPath = "./shell/picToPic_v1.4.sh";
            QStringList arguments;
            arguments << strPicFormat; // 添加参数
            QProcess process;
            process.start(strShellScriptPath,arguments); // 调用Linux Shell脚本

            /*
            // 等待脚本启动
            if (!process.waitForStarted()){
                // 处理错误
                QString strError = process.errorString();
                //qDebug() << strError;
                ui->plainTextEdit->setPlainText("Error starting process:\r\n" + strError + "\r\nTimeout!");
            }
            */

            // 等待脚本结束
            if (!process.waitForFinished(-1))
            {
                // 处理错误
                QString strError = process.errorString();
                //qDebug() << strError;
                ui->plainTextEdit->setPlainText("Error waiting for process to finish:\r\n" + strError + "\r\nTimeout!");
                QMessageBox::critical(this, " 错误提示", " 超时，转换失败！");
            }else{
                QString strOutput = process.readAllStandardOutput();
                //qDebug() << strOutput;
                ui->plainTextEdit->setPlainText(strOutput);
                QMessageBox::information(this, "消息提示", "恭喜，转换成功！");
            }

            process.close();
        }
        else{
            QString strShellScriptPath = "./shell/picToPic_add_text_watermark_v1.4.sh";
            QStringList arguments;
            arguments << strWatermarkText << strWatermarkPosition << strPicFormat; // 添加参数
            QProcess process;
            process.start(strShellScriptPath,arguments); // 调用Linux Shell脚本

            /*
            // 等待脚本启动
            if (!process.waitForStarted()){
                // 处理错误
                QString strError = process.errorString();
                //qDebug() << strError;
                ui->plainTextEdit->setPlainText("Error starting process:\r\n" + strError + "\r\nTimeout!");
            }
            */

            // 等待脚本结束
            if (!process.waitForFinished(-1))
            {
                // 处理错误
                QString strError = process.errorString();
                //qDebug() << strError;
                ui->plainTextEdit->setPlainText("Error waiting for process to finish:\r\n" + strError + "\r\nTimeout!");
                QMessageBox::critical(this, " 错误提示", " 超时，转换失败！");
            }else{
                QString strOutput = process.readAllStandardOutput();
                //qDebug() << strOutput;
                ui->plainTextEdit->setPlainText(strOutput);
                QMessageBox::information(this, "消息提示", "恭喜，转换成功！");
            }

            process.close();
        }
    }


}

//单击“图片转PDF”radioButton按钮消息处理函数
void MainWindow::on_radioButton_PicToPDF_clicked()
{
    ui->radioButton_PicToPic->setChecked(false);
    ui->radioButton_PicToPDF->setChecked(true);
    ui->plainTextEdit->clear();
    ui->radioButton_No->setChecked(true);
}

//单击“图片转图片”radioButton按钮消息处理函数
void MainWindow::on_radioButton_PicToPic_clicked()
{
    ui->radioButton_PicToPDF->setChecked(false);
    ui->radioButton_PicToPic->setChecked(true);
    ui->plainTextEdit->clear();
    ui->comboBox_PicFormat->setCurrentIndex(0);
    ui->lineEdit_WatermarkText->clear();
    ui->radioButton_northwest->setChecked(true);
}

